package com.czy.owner.ui.accountbalance;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.czy.owner.R;
import com.czy.owner.adapter.HeadLineAdapter;
import com.czy.owner.adapter.StoreBalanceAdapter;
import com.czy.owner.api.StoreBalanceApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.HeadLineModel;
import com.czy.owner.entity.StoreBalanceModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.Subscribe;
import com.czy.owner.rxbus.ThreadMode;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.headline.HeadLineActivity;
import com.czy.owner.utils.MyLog;
import com.easyrecycleview.EasyRecyclerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

/**
 * Created by yang on 2017/10/11.
 * 门店余额
 */

public class StoreBalanceActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView easyRecyclerView;
    private Handler handler = new Handler();
    private StoreBalanceAdapter adapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("门店余额");
        RxBus.getDefault().register(this);
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(StoreBalanceActivity.this));
        easyRecyclerView.setAdapterWithProgress(adapter = new StoreBalanceAdapter(StoreBalanceActivity.this));
        easyRecyclerView.setRefreshListener(this);
        onRefresh();
    }

    @Override
    protected void updateViews() {

    }


    private void getData() {
        StoreBalanceApi storeBalanceApi = new StoreBalanceApi(new HttpOnNextListener<List<StoreBalanceModel>>() {
            @Override
            public void onNext(List<StoreBalanceModel> data) {
                MyLog.e("yang", "data.size==" + data.size());
                setData(data);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<StoreBalanceModel>>>() {
                }.getType();
                BaseResultEntity<List<StoreBalanceModel>> cacheList = gson.fromJson(cache, type);
                setData(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        }, this);
        storeBalanceApi.setSession(UserHelper.getInstance().getSession(StoreBalanceActivity.this));
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(storeBalanceApi);
    }


    private void setData(List<StoreBalanceModel> data) {
        adapter.addAll(data);
        if (adapter.getAllData().size() == 0) {
            easyRecyclerView.setEmptyView(R.layout.view_empty_common);
            easyRecyclerView.showEmpty();
        }
    }


    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.clear();
                getData();
            }
        }, 1000);
    }

    @Subscribe(code = Constants.RECHARGE_SUCCESS_CODE, threadMode = ThreadMode.MAIN)
    public void eventRecharge(CommonEvent rechargeEvent) {
        adapter.clear();
        getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unRegister(this);
    }
}
